package cn.ac.cintcm.prespring.etl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.ac.cintcm.prespring.util.Config;
import cn.ac.cintcm.prespring.util.FileUtil;

public class Dictionary {
    private Map<String, Integer> dict;
    
    public Dictionary(String fileName) {
        try {
            List<String> terms = FileUtil.read(fileName);
            dict = parse(terms);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Map<String, Integer> parse(List<String> terms) {
        Map<String, Integer> d = new HashMap<String, Integer>();
        if (terms != null) {
            for (String s : terms) {
                if (s != null) {
                    String[] strs = s.split(Config.ITEM_SPLITTER);
                    d.put(strs[0], Integer.parseInt(strs[1]));
                }
            }
        }
        return d;
    }
    
    public boolean isExist(String str) {
        return dict.containsKey(str);
    }
    
    public Integer getValue(String str) {
        return dict.get(str);
    }
}
